package org.liang.job.repository.mapper;

import java.math.BigInteger;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

import org.liang.job.beans.TriggerInfo;
import org.springframework.jdbc.core.RowMapper;

/**
 * 触发器信息行映射器.
 *
 * @author L.Yang
 * @version 1.0, 2015年10月21日
 */
public class TriggerInfoRowMapper implements RowMapper<TriggerInfo> {

    @Override
    public TriggerInfo mapRow(ResultSet rs, int rowNum) throws SQLException {
        TriggerInfo trigger = new TriggerInfo();
        trigger.setJobId(rs.getString("JOB_ID"));
        trigger.setTitle(rs.getString("TITLE"));
        trigger.setJobGroup(rs.getString("JOB_GROUP"));
        trigger.setJobGroupName(rs.getString("JOB_GROUP_NAME"));
        trigger.setTriggerName(rs.getString("TRIGGER_NAME"));
        trigger.setTriggerGroup(rs.getString("TRIGGER_GROUP"));
        trigger.setTriggerGroupName(rs.getString("TRIGGER_GROUP_NAME"));
        trigger.setPriority(rs.getString("PRIORITY"));
        trigger.setTriggerState(rs.getString("TRIGGER_STATE"));
        trigger.setPrevFireTime(convert2Date(rs, "PREV_FIRE_TIME"));
        trigger.setNextFireTime(convert2Date(rs, "NEXT_FIRE_TIME"));
        trigger.setStartTime(convert2Date(rs, "START_TIME"));
        trigger.setEndTime(convert2Date(rs, "END_TIME"));
        return trigger;
    }

    /**
     * 日期类型转换.
     * 
     * @param rs
     *            结果集
     * @param key
     *            列名
     * @return 日期
     */
    private Date convert2Date(ResultSet rs, String key) {
        try {
            BigInteger bi = rs.getBigDecimal(key).toBigInteger();
            return bi.intValue() == 0 ? null : new Date(bi.longValue());
        } catch (SQLException e) {
            return null;
        }
    }

}
